package com.tarena.lbs.basic.web.service.impl;

import com.alibaba.nacos.api.utils.StringUtils;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.basic.web.repoistory.AdminRepository;
import com.tarena.lbs.basic.web.repoistory.UserRepository;
import com.tarena.lbs.basic.web.service.LoginService;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.enums.Roles;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.LoginBO;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.param.UserLoginParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {
    //仓储层 一个表格,一个仓储层 每个仓储层处理不同数据
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;

    public AdminBO adminDoLogin(AdminLoginParam param) throws BusinessException {
        log.debug("S1-接收控制层参数:{}",param);
        //1. 根据phone电话 查询lbs_admin表格的user对象
        String phone = param.getPhone();
        AdminBO adminBO=adminRepository.getUserByPhone(phone);
        //2. 校验 是否存在
        if (adminBO==null||adminBO.getId()==null){
            throw new BusinessException("100","用户不存在");
        }
        //3. 如果存在 校验密码是否相等
        matchPassword(param.getPassword(),adminBO.getAccountPassword());
        //4. 如果校验都成功 生成JWT 封装查询adminBO返回
        generateJwt(adminBO);
        return adminBO;
    }

    @Override
    public UserBO userDoLogin(UserLoginParam param)throws BusinessException  {
        //1.根据phone电话号 到lbs_user表格查询数据 select * from lbs_user where phone=#{phone}
        UserBO userBO=userRepository.getUserByPhone(param.getPhone());
        //2.判断存在 不存在抛异常
        if(userBO == null || userBO.getId() == null){
            throw new BusinessException("100","用户不存在");
        }
        //3.存在 对比密码 错误抛异常
        matchPassword(param.getPassword(),userBO.getPassword());
        //4.生成JWT 包装到bo 返回
        generateJwt(userBO);
        return userBO;
    }

    /**
     * 生成jwt令牌
     * @param loginBO
     * @throws BusinessException
     */
    private void generateJwt(LoginBO loginBO) throws BusinessException {
        //1.准备好 一个写入jwt的数据对象UserPrinciple 利用adminBO 封装
        UserPrinciple userPrinciple=new UserPrinciple();
        userPrinciple.setId(loginBO.getId());
        userPrinciple.setNickname(loginBO.getNickname());
        //2.根据adminBO中accountType的值 0 给角色Admin 1 给角色SHOP
        Integer roleNum = loginBO.getRoles();//0,1,2
        Roles role=null;
        //TODO 代码瑕疵 if判断数字 提供role赋值 业务强耦合
        if (roleNum==0){
            role=Roles.ADMIN;
        }else if (roleNum==1){
            role=Roles.SHOP;
        }else {
            role=Roles.USER;
        }
        userPrinciple.setRole(role);
        //3.生成JWT 使用jwtEncoder
        String jwt = jwtEncoder.generateToken(userPrinciple);
        //4. adminBO 缺少
        loginBO.setJwt(jwt);
    }

    /**
     * 判断密码是否相同
     * @param password
     * @param accountPassword
     * @throws BusinessException
     */
    private void matchPassword(String password, String accountPassword) throws BusinessException {
        boolean equals = StringUtils.equals(password, accountPassword);
        //只要不相同,抛异常
        if (!equals){
            throw new BusinessException("101","密码错误");
        }
    }
}
